package cn.wen233.sunglow.runner;

import lombok.RequiredArgsConstructor;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.UrlResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

/**
 * 启动类处理器
 *
 * @author wenei
 * @date 2021-10-23 11:39
 */
@Component
@RequiredArgsConstructor
public class StartRunner implements ApplicationRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(StartRunner.class);

    private final JdbcTemplate jdbcTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        LOGGER.info("执行初始化sql脚本");
        try {
            ScriptUtils.executeSqlScript(jdbcTemplate.getDataSource().getConnection(), new UrlResource("classpath:db/schema.sql"));
        } catch (Exception e) {
            LOGGER.warn("数据库已经初始化过");
        }
        LOGGER.info("执行初始化sql脚本完成");
    }
}
